<?php
/*****************************************************************************\
+-----------------------------------------------------------------------------+
| X-Cart                                                                      |
| Copyright (c) 2001-2007 Ruslan R. Fazliev <rrf@rrf.ru>                      |
| All rights reserved.                                                        |
+-----------------------------------------------------------------------------+
| PLEASE READ  THE FULL TEXT OF SOFTWARE LICENSE AGREEMENT IN THE "COPYRIGHT" |
| FILE PROVIDED WITH THIS DISTRIBUTION. THE AGREEMENT TEXT IS ALSO AVAILABLE  |
| AT THE FOLLOWING URL: http://www.x-cart.com/license.php                     |
|                                                                             |
| THIS  AGREEMENT  EXPRESSES  THE  TERMS  AND CONDITIONS ON WHICH YOU MAY USE |
| THIS SOFTWARE   PROGRAM   AND  ASSOCIATED  DOCUMENTATION   THAT  RUSLAN  R. |
| FAZLIEV (hereinafter  referred to as "THE AUTHOR") IS FURNISHING  OR MAKING |
| AVAILABLE TO YOU WITH  THIS  AGREEMENT  (COLLECTIVELY,  THE  "SOFTWARE").   |
| PLEASE   REVIEW   THE  TERMS  AND   CONDITIONS  OF  THIS  LICENSE AGREEMENT |
| CAREFULLY   BEFORE   INSTALLING   OR  USING  THE  SOFTWARE.  BY INSTALLING, |
| COPYING   OR   OTHERWISE   USING   THE   SOFTWARE,  YOU  AND  YOUR  COMPANY |
| (COLLECTIVELY,  "YOU")  ARE  ACCEPTING  AND AGREEING  TO  THE TERMS OF THIS |
| LICENSE   AGREEMENT.   IF  YOU    ARE  NOT  WILLING   TO  BE  BOUND BY THIS |
| AGREEMENT, DO  NOT INSTALL OR USE THE SOFTWARE.  VARIOUS   COPYRIGHTS   AND |
| OTHER   INTELLECTUAL   PROPERTY   RIGHTS    PROTECT   THE   SOFTWARE.  THIS |
| AGREEMENT IS A LICENSE AGREEMENT THAT GIVES  YOU  LIMITED  RIGHTS   TO  USE |
| THE  SOFTWARE   AND  NOT  AN  AGREEMENT  FOR SALE OR FOR  TRANSFER OF TITLE.|
| THE AUTHOR RETAINS ALL RIGHTS NOT EXPRESSLY GRANTED BY THIS AGREEMENT.      |
|                                                                             |
| The Initial Developer of the Original Code is Ruslan R. Fazliev             |
| Portions created by Ruslan R. Fazliev are Copyright (C) 2001-2007           |
| Ruslan R. Fazliev. All Rights Reserved.                                     |
+-----------------------------------------------------------------------------+
\*****************************************************************************/

#
# $Id: cc_webcraft.php,v 1.7.2.4 2007/08/13 12:44:09 zaa Exp $
#

if ( !defined('XCART_START') ) { header("Location: ../"); die("Access denied"); }

# CVV2 check result codes.
$cvv2_err0 = array(
	"0" => "CVV2 not provided.",
	"1" => "CVV2 provided.",
	"2" => "CVV2 not provided.",
	"9" => "CVV2 not provided."
);

$cvv2_err1 = array(
	"Y" => "The card verification was not checked.",
	"U" => "The card verification was not matched.",
	"C" => "Card verification was performed and CVV2 was matched.",
);

if (isset($secure_3d) && isset($secure_3d['data'])) {
	$response_array = $secure_3d['data'];
} else {
	x_load('http');

	$request_account = $module_params["param03"];
	$request_login = $module_params["param01"];
	$request_password = $module_params["param02"];
	$request_prefix = $module_params["param09"];

	$post = array();
	$post[] = "ACCOUNT=". $request_account;
	$post[] = "USERNAME=". $request_login;
	$post[] = "PASSWORD=". $request_password;
	$post[] = "TYPE=100"; # SALE request
	$post[] = "CARDNO=" . $userinfo["card_number"];
	$post[] = "CVV2=" . $userinfo["card_cvv2"];
	$post[] = "EXPIRY=" . substr($userinfo["card_expire"],2,2) . substr($userinfo["card_expire"],0,2);
	$post[] = "AMOUNT=" . $cart["total_cost"] * 100;
	$post[] = "ORDERNO=" . $request_prefix . join("-", $secure_oid);

	$url = $module_params['testmode'] == 'Y' ? "https://psp.stg.transactium.com:443/merchantservices/gateway/process.aspx" : "https://psp.transactium.com:443/merchantservices/gateway/process.aspx";
	list($a, $return) = func_https_request("POST", $url, $post);

	$response_array = array();

	# Check that we got valid response and parse it.
	if (!empty($return) && strpos($return, "ERROR") !== false) {
		parse_str($return, $response_array);
	}
}

if (!empty($response_array) && is_array($response_array)) {
	if ($response_array["ERROR"] == 0) {
		$bill_output["code"] = 1;
		$bill_output["billmes"] = $response_array["ERROR"] . ". " . $response_array["MESSAGE"];
		$bill_output["billmes"] = $response_array["MESSAGE"];
	} else {
		$bill_output["code"] = 2;
		$bill_output["billmes"] = str_replace("_", " ", trim($response_array["MESSAGE"])) . " (Error Code: " . $response_array['ERROR'].")";
	}

	# Save transaction response details.
	$details_fields = array('AUTHCODE', 'BATCH', 'HOSTCODE', 'TIME', 'TRANSID', 'TYPE', '3DSECUREENROLLED', 'ECI');
	foreach ($details_fields as $field) {
		if (isset($response_array[$field]) && !zerolen($response_array[$field])) {
			$bill_output['billmes'] .= "; $field: " . $response_array[$field];
		}
	}

	# Record CVV2 check result.
	if (isset($response_array['CVV2']) && strlen($response_array['CVV2']) == 2) {
		$cvv2_code = substr($response_array['CVV2'], 0, 1);
		$cvv2_result = substr($response_array['CVV2'], 1, 1);
		$bill_output['cvvmes'] = '';
		if (in_array($cvv2_code, array_keys($cvv2_err0))) {
			$bill_output['cvvmes'] .= $cvv2_err0[$cvv2_code];
		}
		if (in_array($cvv2_result, array_keys($cvv2_err1))) {
			$bill_output['cvvmes'] .= " " . $cvv2_err1[$cvv2_result];
		}
	}
} else {
	$bill_output["code"] = 2;
	$bill_output["billmes"] = "Error: Failed to receive response from Transactium payment gateway system.";
}
?>
